import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/extension/date_string_extension.dart';
import 'package:xvideos/hj_model/mine/promotion_record.dart';
import 'package:xvideos/hj_utils/date_time_util.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/tools_base/loading/l_error_widget.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';
import 'package:xvideos/tools_base/refresh/pull_refresh.dart';
import 'package:xvideos/tools_base/widget/net_image_widget.dart';

import 'mine_share_record_logic.dart';

class MineShareRecordPage extends StatelessWidget {
  const MineShareRecordPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MineShareRecordLogic>(
      init: MineShareRecordLogic(),
      builder: (controller) {
        return Scaffold(
          appBar: AppBar(title: Text("邀请记录")),
          body: Column(
            children: [
              12.sizeBoxH,
              Text('已邀请：${controller.model?.totalInviteUserCount ?? 0}人', style: textStyle(12, Color(0xff525252), FontWeight.w400)),
              Expanded(
                child: pullYsRefresh(
                  onInit: (ctr) => controller.refreshController = ctr,
                  onLoading: (refreshController) => controller.loadData(isRefresh: false),
                  onRefresh: (refreshController) => controller.loadData(),
                  child: () {
                    if (controller.isLoading) return LoadingCenterWidget();
                    if (controller.promotionList.isEmpty) return CErrorWidget();
                    return ListView.separated(
                        padding: EdgeInsets.symmetric(horizontal: 16, vertical: 12),
                        shrinkWrap: true,
                        itemCount: controller.promotionList.length,
                        separatorBuilder: (context, index) => Divider(
                              height: 0.5,
                              color: Colors.white.withOpacity(.1),
                            ),
                        itemBuilder: (BuildContext context, int index) {
                          return _buildItemView(index, controller.promotionList[index]);
                        });
                  }(),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  _buildItemView(int index, Promotion item) {
    return Container(
      height: 30,
      width: double.infinity,
      margin: EdgeInsets.symmetric(vertical: 12),
      child: Row(
        children: [
          NetworkImageLoader(
            imageUrl: item.portrait ?? '',
            width: 30,
            height: 30,
            borderRadius: 30,
          ),
          11.sizeBoxW,
          Text(
            item.name ?? '',
            style: TextStyle(color: Color(0xffffffff), fontWeight: FontWeight.w500, fontSize: 14.0),
          ),
          Spacer(),
          Text(
            '注册时间: ${item.createAt.utcTimeSToYMDString(gap: '/')}',
            style: TextStyle(color: Color(0xff989898), fontWeight: FontWeight.w400, fontSize: 12.0),
          )
        ],
      ),
    );
  }

  _buildTitleView(int flex, String text) {
    return Expanded(
      flex: flex,
      child: Center(
        child: Text(
          text,
          style: TextStyle(color: Color(0xffffffff), fontWeight: FontWeight.w400, fontSize: 14.0),
        ),
      ),
    );
  }

  _buildT2View(int flex, String text) {
    return Expanded(
      flex: flex,
      child: Center(
        child: Text(
          text,
          style: TextStyle(color: Color(0xffacbabf), fontWeight: FontWeight.w400, fontSize: 12.0),
        ),
      ),
    );
  }
}
